package cn.itcast.web.quartz;

import cn.itcast.domain.cargo.Export;
import cn.itcast.domain.cargo.ExportExample;
import cn.itcast.domain.vo.ExportResult;
import cn.itcast.service.cargo.ExportService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

//任务类
public class MyJob {

    @Reference
    ExportService exportService;


    //打印系统的当前时间
    public void printTime(){
        System.out.println("当前时间为:"+ DateFormatUtils.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
    }

    //查询报运单结果
    public void queryResult(){
        ExportExample exportExample = new ExportExample();
        ExportExample.Criteria criteria = exportExample.createCriteria();
        criteria.andStateEqualTo(1L);
        // 查询所有状态为"已上报"的报运单
        List<Export> list = exportService.findAll(exportExample);

        if (list!=null && list.size()>0) {
            System.out.println("查询到:"+list.size()+"个");
            for (Export export : list) {
                //去报运接口中查询有无处理改订单
                WebClient wc = WebClient.create("http://localhost:9999/ws/export/user/" + export.getId());

                //若查询到,就改结果; 若查询不到,转换不成ExportResult对象会有异常
                try {
                    ExportResult exportResult = wc.get(ExportResult.class);
                    exportService.updateByExportResult(exportResult);
                    System.out.println(export.getId()+"报运结果已更新");
                } catch (Exception e) {
                }
            }
        }
    }
}
